package org.smile.json;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.smile.exception.JSONParseException;
import org.smile.json.parser.JSONTokener;

/**
 * 
 * @author 胡真山
 *
 */
@SuppressWarnings({ "serial", "unchecked" })
public class JSONArray extends ArrayList implements JSONAware{
	/**
	 * 转换列表转换成一个json字符串
	 * @param list
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String toJSONString(Collection list){
		if(list == null)
			return "null";
        boolean first = true;
        StringBuffer sb = new StringBuffer();
		Iterator iter=list.iterator();
        sb.append('[');
		while(iter.hasNext()){
            if(first)
                first = false;
            else
                sb.append(',');
            
			Object value=iter.next();
			if(value == null){
				sb.append("null");
				continue;
			}
			sb.append(JSONValue.toJSONString(value));
		}
        sb.append(']');
		return sb.toString();
	}
	/**
	 * 转换数组
	 * @param array
	 * @return
	 */
	public static String toJSONString(Object[] array){
		if(array == null)
			return "null";
        boolean first = true;
        StringBuffer sb = new StringBuffer();
        
        sb.append('[');
        for(int i=0;i<array.length;i++){
            if(first)
                first = false;
            else
                sb.append(',');
            
			Object value=array[i];
			if(value == null){
				sb.append("null");
				continue;
			}
			sb.append(JSONValue.toJSONString(value));
		}
        sb.append(']');
		return sb.toString();
	}
	/**
	 * 空的json对象
	 */
	public JSONArray(){}
	/**
	 * 从一个json字符串构造一个jsonArray对象
	 * @param jsonStr JSON字符串
	 * @throws JSONParseException 对字符串不能转成json对象时抛出异常
	 */
	public JSONArray(String jsonStr) throws JSONParseException{
		this(new JSONTokener(jsonStr));
	}
	/**
	 * 从一个jsontokener实例化一个jsonarray对象
	 * @param x
	 * @throws JSONParseException
	 */
	public JSONArray(JSONTokener x) throws JSONParseException {
        if (x.nextClean() != '[') {
            throw x.syntaxError("A JSONArray text must start with '['");
        }
        if (x.nextClean() != ']') {
            x.back();
            for (;;) {
                if (x.nextClean() == ',') {
                    x.back();
                    this.add(null);
                } else {
                    x.back();
                    this.add(x.nextValue());
                }
                switch (x.nextClean()) {
                case ';':
                case ',':
                    if (x.nextClean() == ']') {
                        return;
                    }
                    x.back();
                    break;
                case ']':
                    return;
                default:
                    throw x.syntaxError("Expected a ',' or ']'");
                }
            }
        }
    }
	
	public String toJSONString(){
		return toJSONString(this);
	}
	/**
	 * 写入到一个writer中
	 * @param writer
	 * @throws IOException
	 */
	public void write(Writer writer) throws IOException{
		JSONValue.write(this,writer);
	}
	/**
	 * 写入到一个writer中
	 * @param writer
	 * @throws IOException
	 */
	public static void write(Object json,Writer writer) throws IOException{
		JSONValue.write(json,writer);
	}
	/**
	 * 重写toString方法
	 */
	public String toString() {
		return toJSONString();
	}
}
